var express = require('express');
var axios = require('axios');
var router = express.Router();
var newList = new Array();
// var qs = require('qs');

router.get('/', function (req, res, next) {
    let {test_number} = req.query
    console.log("重新请求")
    // res.send("考试习题页面");
    var config = {
        method: 'post',
        baseURL: 'http://localhost:9023',
        url: '/student_work/containTest/getExamWord',
        params: {
            studentId: req.session.userId,
            testNumber:test_number
        },
    }
    axios(config)
        .then(function (response) {
            newList.length = 0
            newList.push(response.data.data);
            newList.push(response.data.otherData);
            console.log(response.data);
            if (response.data.code == 0) {
                console.log("获取试卷信息成功");
                res.render('webPage/exam', {
                    list: newList
                });
            } else {
                // console.log(response);
                res.render('webPage/notFound');
                // res.send({ err: -1, msg: "获取试卷信息失败" })
                console.log("获取试卷信息失败")
                console.log(testNumber)
            }
        })
        .catch(function (error) {
            // console.log(error);
            res.render('webPage/notFound');
            console.log("服务器错误")
        });
})



router.post('/updata', (req, res) => {
    let {answerData} = req.body
    let {test_number} = req.query
    console.log(answerData)
    var config = {
        method: 'post',
        baseURL: 'http://localhost:9023',
        url: '/student_work/upExamWord',
        data: {
            studentId: req.session.userId,
            answerData:answerData,
            testNumber: req.session.testNumberRel
        },
    }
    axios(config)
        .then(function (response) {
            if (response.data.code=='success'){
                res.redirect('/admin/index');//成功后回到主页
            }else{
                res.redirect('/admin/exam');
            }console.log(response)
        })
        .catch(function (error) {
            console.log(error);
            console.log("失败")
        });
    console.log(typeof (answerData))
})

/**
 * @api {post} /admin/exam/MYExam 渲染公共部分
 * @apiName public
 * @apiGroup admin
 *
 *
 * @apiSuccess {String} firstname Firstname of the User.
 * @apiSuccess {String} lastname  Lastname of the User.
 */
router.post('/MYExam', (req, res, next) => {
    var user ={
        userName:req.session.userName,
    }
    res.send({ err: 0, msg: '',data:req.session.userName })
})

module.exports = router;
